package com.ruoyi.smartcity.huanjing.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.smartcity.huanjing.domain.HuanjingShuizhi;
import com.ruoyi.smartcity.huanjing.mapper.HuanjingShuizhiMapper;
import com.ruoyi.smartcity.huanjing.service.IHuanjingShuizhiService;
import com.ruoyi.smartcity.traffic.vo.EchartsOptionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 水质Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class HuanjingShuizhiServiceImpl implements IHuanjingShuizhiService {
    @Autowired
    private HuanjingShuizhiMapper huanjingShuizhiMapper;

    /**
     * 查询水质
     *
     * @param id 水质主键
     * @return 水质
     */
    @Override
    public HuanjingShuizhi selectHuanjingShuizhiById(Long id) {
        return huanjingShuizhiMapper.selectHuanjingShuizhiById(id);
    }

    /**
     * 查询水质列表
     *
     * @param huanjingShuizhi 水质
     * @return 水质
     */
    @Override
    public List<HuanjingShuizhi> selectHuanjingShuizhiList(HuanjingShuizhi huanjingShuizhi) {
        return huanjingShuizhiMapper.selectHuanjingShuizhiList(huanjingShuizhi);
    }

    /**
     * 新增水质
     *
     * @param huanjingShuizhi 水质
     * @return 结果
     */
    @Override
    public int insertHuanjingShuizhi(HuanjingShuizhi huanjingShuizhi) {
        huanjingShuizhi.setCreateTime(DateUtils.getNowDate());
        return huanjingShuizhiMapper.insertHuanjingShuizhi(huanjingShuizhi);
    }

    /**
     * 修改水质
     *
     * @param huanjingShuizhi 水质
     * @return 结果
     */
    @Override
    public int updateHuanjingShuizhi(HuanjingShuizhi huanjingShuizhi) {
        huanjingShuizhi.setUpdateTime(DateUtils.getNowDate());
        return huanjingShuizhiMapper.updateHuanjingShuizhi(huanjingShuizhi);
    }

    /**
     * 批量删除水质
     *
     * @param ids 需要删除的水质主键
     * @return 结果
     */
    @Override
    public int deleteHuanjingShuizhiByIds(Long[] ids) {
        return huanjingShuizhiMapper.deleteHuanjingShuizhiByIds(ids);
    }

    /**
     * 删除水质信息
     *
     * @param id 水质主键
     * @return 结果
     */
    @Override
    public int deleteHuanjingShuizhiById(Long id) {
        return huanjingShuizhiMapper.deleteHuanjingShuizhiById(id);
    }

    @Override
    public EchartsOptionVO shuizhi() {
        EchartsOptionVO echartsOptionVO = new EchartsOptionVO();
        List<HuanjingShuizhi> list = huanjingShuizhiMapper.selectHuanjingShuizhiList(new HuanjingShuizhi());
        if (list != null && !list.isEmpty()) {
            JSONArray yData = new JSONArray();
            JSONArray xData = new JSONArray();
            for (HuanjingShuizhi item : list) {
                xData.add(item.getName());
                yData.add(item.getMonitorValue());
                echartsOptionVO.setXData(xData);
                echartsOptionVO.setYData(yData);
            }
        }
        return echartsOptionVO;
    }
}
